<?php

namespace app\repositories\eloquent;
use app\admin\model\AdminRoot;
use app\repositories\contracts\AdminLogInterface;
use app\admin\model\AdminLog;
use think\response\Json;
use function foo\func;

/***
 * Class AdminLogProvider
 * @package app\repositories\eloquent
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:11
 * @Description 实现后台日志接口
 */
class AdminLogProvider implements AdminLogInterface
{
    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取日志分页列表
     */
    public function getAdminLogListPage($page,$limit,$orderby='id',$order='asc'):array
    {
        return AdminLog::limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取日志总数据量
     */
    public function getAdminLogListCount():int
    {
        return AdminLog::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取日志列表
     */
    public function getAdminLogList($orderby='id',$order='asc'):array
    {
        return AdminLog::order($orderby, $order)->select()->toArray();
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除日志
     */
    public function delAdminLogList(array $id):bool
    {
        $list = AdminLog::select($id);
        $result = $list->delete();
        return $result;
    }



    /***
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增日志
     */
    public function addAdminLogList($admin_id,$input,$path,$method,$ip)
    {
        $admin = AdminRoot::where('id',$admin_id)->find();
        if($admin) {
            $admin_id = $admin['id'];
            $admin_name = $admin['root_name'];
        }else{
            return false;
        }
        $log = new AdminLog;
        $log->user_id = $admin_id;
        $log->user_name = $admin_name;
        $log->method = $method;
        $log->ip_address = $ip;
        $log->content = " 路径：".$path." 参数：".json_encode($input, JSON_UNESCAPED_UNICODE);
        $log->created_time = time();
        return $log->save();
    }

    /***
     * @param $by
     * @Author Vee7
     * @Date 2021/12/22
     * @Time 17:18
     * @Description 获取接口的/年/月/周/日/时数据
     */
    public function getAdminLogListCountWhere($by,$time='-1 hours')
    {
        switch ($by)
        {
            case 'year':
                return AdminLog::whereYear('create_time')->select()->toArray();
            break;
            case 'month':
                return AdminLog::whereMonth('create_time')->select()->toArray();
            break;
            case 'day':
                return AdminLog::whereDay('create_time')->select()->toArray();
            break;
            case 'week':
                return AdminLog::whereWeek('create_time')->field('create_time')->order('create_time','asc')->select()->toArray();
            break;
            case 'time':
                return AdminLog::whereTime('create_time',$time)->select()->toArray();
            break;
            //默认查询近一小时
            default:
                return AdminLog::whereTime('create_time','-1 hours')->select()->toArray();
        }
    }

}